package com.cicadasmall.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cicadasmall.common.base.BaseService;
import com.cicadasmall.common.func.Fn;
import com.cicadasmall.common.resp.R;
import com.cicadasmall.system.dto.PositionInputDTO;
import com.cicadasmall.system.dto.PositionQueryDTO;
import com.cicadasmall.system.dto.PositionUpdateDTO;
import com.cicadasmall.data.domain.PositionDO;
import com.cicadasmall.data.mapper.SysPositionMapper;
import com.cicadasmall.system.service.IPositionService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 职位表 服务实现类
 * </p>
 *
 * @author jin
 * @since 2020-08-25
 */
@Service("PositionServiceImpl")
public class PositionServiceImpl extends BaseService<SysPositionMapper, PositionDO> implements IPositionService {

    @Override
    public List<PositionDO> findByUserId(Serializable userId) {
        return baseMapper.selectByUserId(userId);
    }

    @Override
    public R page(PositionQueryDTO positionQueryDTO) {
        LambdaQueryWrapper<PositionDO> lambdaQueryWrapper = getLambdaQueryWrapper().orderByAsc(PositionDO::getSortId);
        if (Fn.isNotEmpty(positionQueryDTO.getPostName())) {
            lambdaQueryWrapper.like(PositionDO::getPostName, positionQueryDTO.getPostName());
        }
        return R.ok(baseMapper.selectPage(positionQueryDTO.page(), lambdaQueryWrapper));
    }

    @Override
    public R save(PositionInputDTO positionInputDTO) {
        PositionDO position = positionInputDTO.convertToEntity();
        baseMapper.insert(position);
        return R.ok(true);
    }

    @Override
    public R update(PositionUpdateDTO positionUpdateDTO) {
        PositionDO position = positionUpdateDTO.convertToEntity();
        baseMapper.updateById(position);
        return R.ok(true);
    }

    @Override
    public R findById(Serializable id) {
        PositionDO position = baseMapper.selectById(id);
        return R.ok(position);
    }

    @Override
    public R deleteById(Serializable id) {
        baseMapper.deleteById(id);
        return R.ok(true);
    }

}
